﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Lyon.Utility
{
    /// <summary>
    /// 系统工具。
    /// </summary>
    public class SystemUtility
    {
        /// <summary>
        /// 判读是否在管理员权限下运行。
        /// </summary>
        /// <returns></returns>
        static public bool IsRunAdmin()
        {
            //获得当前登录的Windows用户标示
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
            return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
        }
        /// <summary>
        /// 以管理员权限运行。
        /// </summary>
        /// <param name="args"></param>
        static public void RunAdmin(string[] args)
        {
            Process currentProcess = Process.GetCurrentProcess();
            //创建启动对象
            ProcessStartInfo startInfo = new ProcessStartInfo();
            //设置运行文件
            startInfo.FileName = currentProcess.MainModule.FileName;
            //设置启动参数
            startInfo.Arguments = String.Join(" ", args);
            //设置启动动作,确保以管理员身份运行
            startInfo.Verb = "runas";
            //如果不是管理员，则启动UAC
            System.Diagnostics.Process.Start(startInfo);
            //退出
            currentProcess.Kill();
        }
    }

    
}
